home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / bash / bash_110 / mint / bash110s.zoo / bash-1.10 / bashline.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-11  |  31.1 KB  |  1,234 lines

  1. /* bashline.c -- Bash's interface to the readline library. */
  2.  
  3. /* Copyright (C) 1987,1991 Free Software Foundation, Inc.
  4.  
  5.    This file is part of GNU Bash, the Bourne Again SHell.
  6.  
  7.    Bash is free software; you can redistribute it and/or modify it
  8.    under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 1, or (at your option)
  10.    any later version.
  11.  
  12.    Bash is distributed in the hope that it will be useful, but WITHOUT
  13.    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14.    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
  15.    License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with Bash; see the file COPYING.  If not, write to the Free
  19.    Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  20.  
  21. #include <stdio.h>
  22. #include <readline/readline.h>
  23. #include <readline/history.h>
  24. #include "shell.h"
  25. #include "builtins.h"
  26.  
  27. /* Functions bound to keys in Readline for Bash users. */
  28. static void
  29.   shell_expand_line (), insert_last_arg (), display_shell_version (),
  30.   operate_and_get_next ();
  31.  
  32. /* Helper functions for Readline. */
  33. static void
  34.   bash_symbolic_link_hook (), filename_completion_ignore (), bash_push_line ();
  35.  
  36. static char
  37.   **attempt_shell_completion (), *variable_completion_function (),
  38.   *hostname_completion_function (), *command_word_completion_function ();
  39.  
  40. static void
  41.   snarf_hosts_from_file (), add_host_name (), sort_hostname_list ();
  42.  
  43. /* Externally defined functions used by this file. */
  44. extern char
  45.   *get_string_value (), *filename_completion_function (),
  46.   *username_completion_function ();
  47.  
  48. extern int
  49.   show_shell_version ();
  50.  
  51. /* SPECIFIC_COMPLETION_FUNCTIONS specifies that we have individual
  52.    completion functions which indicate what type of completion should be
  53.    done (at or before point) that can be bound to key sequences with
  54.    the readline library. */
  55. #define SPECIFIC_COMPLETION_FUNCTIONS
  56.  
  57. #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
  58. static void
  59.   bash_specific_completion (),
  60.   bash_complete_filename (), bash_possible_filename_completions (),
  61.   bash_complete_filename_internal (),
  62.   bash_complete_username (), bash_possible_username_completions (),
  63.   bash_complete_username_internal (),
  64.   bash_complete_hostname (), bash_possible_hostname_completions (),
  65.   bash_complete_hostname_internal (),
  66.   bash_complete_variable (), bash_possible_variable_completions (),
  67.   bash_complete_variable_internal (),
  68.   bash_complete_command (), bash_possible_command_completions (),
  69.   bash_complete_command_internal ();
  70. #endif /* SPECIFIC_COMPLETION_FUNCTIONS */
  71.  
  72. /* Called once from parse.y if we are going to use readline. */
  73. initialize_readline ()
  74. {
  75.   rl_terminal_name = get_string_value ("TERM");
  76.   rl_instream = stdin, rl_outstream = stderr;
  77.   rl_special_prefixes = "$@%";
  78.  
  79.   /* Allow conditional parsing of the ~/.inputrc file. */
  80.   rl_readline_name = "Bash";
  81.  
  82.   /* Bind up our special shell functions. */
  83.   rl_add_defun
  84.     ("shell-expand-line", (Function *)shell_expand_line, META(CTRL('E')));
  85.  
  86.   rl_add_defun
  87.     ("insert-last-argument", (Function *)insert_last_arg, META('.'));
  88.  
  89.   rl_bind_key (META('_'), (Function *)insert_last_arg);
  90.  
  91.   rl_add_defun
  92.     ("operate_and_get_next", (Function *)operate_and_get_next, CTRL('O'));
  93.  
  94.   rl_add_defun
  95.     ("display-shell-version", (Function *)display_shell_version, -1);
  96.  
  97.   rl_bind_key_in_map
  98.     (CTRL ('V'), (Function *)display_shell_version, emacs_ctlx_keymap);
  99.  
  100.   /* In Bash, the user can switch editing modes with "set -o [vi emacs]",
  101.      so it is not necessary to allow C-M-j for context switching.  Turn
  102.      off this occasionally confusing behaviour. */
  103.   rl_unbind_key_in_map (CTRL('J'), emacs_meta_keymap);
  104.  
  105. #ifdef SPECIFIC_COMPLETION_FUNCTIONS
  106.   rl_add_defun ("complete-filename", bash_complete_filename, META('/'));
  107.   rl_add_defun ("possible-filename-completions",
  108.         bash_possible_filename_completions, -1);
  109.   rl_bind_key_in_map ('/', bash_possible_filename_completions,
  110.               emacs_ctlx_keymap);
  111.  
  112.   rl_add_defun ("complete-username", bash_complete_username, META('~'));
  113.   rl_add_defun ("possible-username-completions",
  114.         bash_possible_username_completions, -1);
  115.   rl_bind_key_in_map ('~', bash_possible_username_completions,
  116.               emacs_ctlx_keymap);
  117.  
  118.   rl_add_defun ("complete-hostname", bash_complete_hostname, META('@'));
  119.   rl_add_defun ("possible-hostname-completions",
  120.         bash_possible_hostname_completions, -1);
  121.   rl_bind_key_in_map ('@', bash_possible_hostname_completions,
  122.               emacs_ctlx_keymap);
  123.  
  124.   rl_add_defun ("complete-variable", bash_complete_variable, META('$'));
  125.   rl_add_defun ("possible-variable-completions",
  126.         bash_possible_variable_completions, -1);
  127.   rl_bind_key_in_map ('$', bash_possible_variable_completions,
  128.               emacs_ctlx_keymap);
  129.  
  130.   rl_add_defun ("complete-command", bash_complete_command, META('!'));
  131.   rl_add_defun ("possible-command-completions",
  132.         bash_possible_command_completions, -1);
  133.   rl_bind_key_in_map ('!', bash_possible_command_completions,
  134.               emacs_ctlx_keymap);
  135.  
  136. #endif  /* SPECIFIC_COMPLETION_FUNCTIONS */
  137.  
  138.   /* Tell the completer that we want a crack first. */
  139.   rl_attempted_completion_function = (Function *)attempt_shell_completion;
  140.  
  141.   /* Tell the completer that we might want to follow symbolic links. */
  142.   rl_symbolic_link_hook = (Function *)bash_symbolic_link_hook;
  143.  
  144.   /* Tell the filename completer we want a chance to ignore some names. */
  145.   rl_ignore_some_completions_function =
  146.     (Function *)filename_completion_ignore;
  147. }
  148.  
  149. /* On Sun systems at least, rl_attempted_completion_function can end up
  150.    getting set to NULL, and rl_completion_entry_function set to do command
  151.    word completion if Bash is interrupted while trying to complete a command
  152.    word.  This just resets all the completion functions to the right thing.
  153.    It's called from throw_to_top_level(). */
  154. bashline_reinitialize ()
  155. {
  156.   extern void tilde_initialize ();
  157.  
  158.   tilde_initialize ();
  159.   rl_attempted_completion_function = (Function *)attempt_shell_completion;
  160.   rl_symbolic_link_hook = (Function *)bash_symbolic_link_hook;
  161. }
  162.  
  163. /* Contains the line to push into readline. */
  164. static char *push_to_readline = (char *)NULL;
  165.  
  166. /* Push the contents of push_to_readline into the
  167.    readline buffer. */
  168. static void
  169. bash_push_line ()
  170. {
  171.   if (push_to_readline)
  172.     {
  173.       rl_insert_text (push_to_readline);
  174.       free (push_to_readline);
  175.       push_to_readline = (char *)NULL;
  176.     }
  177. }
  178.  
  179. /* Call this to set the initial text for the next line to read
  180.    from readline. */
  181. int
  182. bash_re_edit (line)
  183.      char *line;
  184. {
  185.   if (push_to_readline)
  186.     free (push_to_readline);
  187.  
  188.   push_to_readline = savestring (line);
  189.   rl_startup_hook = (Function *)bash_push_line;
  190.  
  191.   return (0);
  192. }
  193.  
  194. static void
  195. display_shell_version (count, c)
  196.      int count, c;
  197. {
  198.   crlf ();
  199.   show_shell_version ();
  200.   putc ('\r', rl_outstream);
  201.   fflush (rl_outstream);
  202.   rl_on_new_line ();
  203.   rl_redisplay ();
  204. }
  205.  
  206. /* **************************************************************** */
  207. /*                                    */
  208. /*                 Readline Stuff                */
  209. /*                                    */
  210. /* **************************************************************** */
  211.  
  212. /* If the user requests hostname completion, then simply build a list
  213.    of hosts, and complete from that forever more. */
  214. #if !defined (ETCHOSTS)
  215. #define ETCHOSTS "/etc/hosts"
  216. #endif
  217.  
  218. /* The kept list of hostnames. */
  219. static char **hostname_list = (char **)NULL;
  220.  
  221. /* The physical size of the above list. */
  222. static int hostname_list_size = 0;
  223.  
  224. /* The length of the above list. */
  225. static int hostname_list_length = 0;
  226.  
  227. /* Whether or not HOSTNAME_LIST has been initialized. */
  228. int hostname_list_initialized = 0;
  229.  
  230. /* Non-zero means that HOSTNAME_LIST needs to be sorted. */
  231. static int hostname_list_needs_sorting = 0;
  232.  
  233. /* Initialize the hostname completion table. */
  234. static void
  235. initialize_hostname_list ()
  236. {
  237.   char *temp = get_string_value ("hostname_completion_file");
  238.  
  239.   if (!temp)
  240.     temp = ETCHOSTS;
  241.  
  242.   snarf_hosts_from_file (temp);
  243.   sort_hostname_list ();
  244.   if (hostname_list)
  245.     hostname_list_initialized++;
  246. }
  247.  
  248. /* Add NAME to the list of hosts. */
  249. static void
  250. add_host_name (name)
  251.      char *name;
  252. {
  253.   if (hostname_list_length + 2 > hostname_list_size)
  254.     {
  255.       hostname_list = (char **)
  256.     xrealloc (hostname_list,
  257.           (1 + (hostname_list_size += 100)) * sizeof (char *));
  258.     }
  259.  
  260.   hostname_list[hostname_list_length] = savestring (name);
  261.   hostname_list[++hostname_list_length] = (char *)NULL;
  262.   hostname_list_needs_sorting++;
  263. }
  264.  
  265. /* After you have added some names, you should sort the list of names. */
  266. static void
  267. sort_hostname_list ()
  268. {
  269.   if (hostname_list_needs_sorting && hostname_list)
  270.     sort_char_array (hostname_list);
  271.   hostname_list_needs_sorting = 0;
  272. }
  273.  
  274. #define cr_whitespace(c) ((c) == '\r' || (c) == '\n' || whitespace(c))
  275.  
  276. static void
  277. snarf_hosts_from_file (filename)
  278.      char *filename;
  279. {
  280.   FILE *file = fopen (filename, "r");
  281.   char *temp, buffer[256], name[256];
  282.   register int i, start;
  283.  
  284.   if (!file)
  285.     return;
  286.  
  287.   while (temp = fgets (buffer, 255, file))
  288.     {
  289.       /* Skip to first character. */
  290.       for (i = 0; buffer[i] && cr_whitespace (buffer[i]); i++);
  291.  
  292.       /* If comment, ignore. */
  293.       if (buffer[i] == '#')
  294.     continue;
  295.  
  296.       /* If `preprocessor' directive, do the include. */
  297.       if (strncmp (&buffer[i], "$include ", 9) == 0)
  298.     {
  299.       char *includefile = &buffer[i + 9];
  300.       char *t;
  301.  
  302.       /* Find start of filename. */
  303.       while (*includefile && whitespace (*includefile))
  304.         includefile++;
  305.  
  306.       t = includefile;
  307.  
  308.       /* Find end of filename. */
  309.       while (*t && !cr_whitespace (*t))
  310.         t++;
  311.  
  312.       *t = '\0';
  313.  
  314.       snarf_hosts_from_file (includefile);
  315.       continue;
  316.     }
  317.  
  318.       /* Skip internet address. */
  319.       for (; buffer[i] && !cr_whitespace (buffer[i]); i++);
  320.  
  321.       /* Gobble up names.  Each name is separated with whitespace. */
  322.       while (buffer[i] && buffer[i] != '#')
  323.     {
  324.       for (; i && cr_whitespace (buffer[i]); i++);
  325.       if (buffer[i] ==  '#')
  326.         continue;
  327.       for (start = i; buffer[i] && !cr_whitespace (buffer[i]); i++);
  328.       if ((i - start) == 0)
  329.         continue;
  330.       strncpy (name, buffer + start, i - start);
  331.       name[i - start] = '\0';
  332.       add_host_name (name);
  333.     }
  334.     }
  335.   fclose (file);
  336. }
  337.  
  338. /* Return a NULL terminated list of hostnames which begin with TEXT.
  339.    Initialize the hostname list the first time if neccessary.
  340.    The array is malloc ()'ed, but not the individual strings. */
  341. static char **
  342. hostnames_matching (text)
  343.      char *text;
  344. {
  345.   register int i, len = strlen (text);
  346.   register int begin, end;
  347.   int last_search = -1;
  348.   char **result = (char **)NULL;
  349.  
  350.   if (!hostname_list_initialized)
  351.     {
  352.       initialize_hostname_list ();
  353.  
  354.       if (!hostname_list_initialized)
  355.     return ((char **)NULL);
  356.     }
  357.  
  358.   sort_hostname_list ();
  359.  
  360.   /* The list is sorted.  Do a binary search on it for the first character
  361.      in TEXT, and then grovel the names of interest. */
  362.   begin = 0; end = hostname_list_length;
  363.  
  364.   /* Special case.  If TEXT consists of nothing, then the whole list is
  365.      what is desired. */
  366.   if (!*text)
  367.     {
  368.       result = (char **)xmalloc ((1 + hostname_list_length) * sizeof (char *));
  369.       for (i = 0; i < hostname_list_length; i++)
  370.     result[i] = hostname_list[i];
  371.       result[i] = (char *)NULL;
  372.       return (result);
  373.     }
  374.  
  375.   /* Scan until found, or failure. */
  376.   while (end != begin)
  377.     {
  378.       int r = 0;
  379.  
  380.       i = ((end - begin) / 2) + begin;
  381.       if (i == last_search)
  382.     break;
  383.  
  384.       if (hostname_list[i] &&
  385.       (r = strncmp (hostname_list[i], text, len)) == 0)
  386.     {
  387.       while (strncmp (hostname_list[i], text, len) == 0 && i) i--;
  388.       if (strncmp (hostname_list[i], text, len) != 0) i++;
  389.  
  390.       begin = i;
  391.       while (hostname_list[i] &&
  392.          strncmp (hostname_list[i], text, len) == 0) i++;
  393.       end = i;
  394.  
  395.       result = (char **)xmalloc ((1 + (end - begin)) * sizeof (char *));
  396.       for (i = 0; i + begin < end; i++)
  397.         result[i] = hostname_list[begin + i];
  398.       result[i] = (char *)NULL;
  399.       return (result);
  400.     }
  401.  
  402.       last_search = i;
  403.  
  404.       if (r < 0)
  405.     begin = i;
  406.       else
  407.     end = i;
  408.     }
  409.   return ((char **)NULL);
  410. }
  411.  
  412. /* This is a K*rn shell style insert-last-arg function.  The
  413.    difference is that Bash puts stuff into the history file before
  414.    expansion and file name generation, so we deal with exactly what the
  415.    user typed.  Those wanting the other behavior, at least for the last
  416.    arg, can use `$_'.  This also `knows' about how rl_yank_nth_arg treats
  417.    `$'. */
  418. static void
  419. insert_last_arg (count, c)
  420.      int count, c;
  421. {
  422.   extern int rl_explicit_arg;
  423.  
  424.   if (rl_explicit_arg)
  425.     rl_yank_nth_arg (count, c);
  426.   else
  427.     rl_yank_nth_arg ('$', c);
  428. }
  429.  
  430. /* The equivalent of the K*rn shell C-o operate-and-get-next-history-line
  431.    editing command. */
  432. static int saved_history_line_to_use = 0;
  433.  
  434. static void
  435. set_saved_history ()
  436. {
  437.   HIST_ENTRY *h;
  438.  
  439.   if (saved_history_line_to_use)
  440.     {
  441.       if (history_set_pos (saved_history_line_to_use))
  442.     {
  443.       h = current_history ();
  444.       if (h)
  445.         {
  446.           rl_insert_text (h->line);
  447.           /*
  448.            * Get rid of any undo list created by the previous insert,
  449.            * so the line won't totally be erased when the edits are
  450.            * undone (they will be normally, because this is a history
  451.            * line -- cf. readline.c: line 380 or so).
  452.            */
  453.           if (rl_undo_list)
  454.         {
  455.           free_undo_list ();
  456.           rl_undo_list = (UNDO_LIST *)NULL;
  457.         }
  458.         }
  459.     }
  460.     }
  461.   saved_history_line_to_use = 0;
  462.   rl_startup_hook = (Function *)NULL;
  463. }  
  464.  
  465. static void
  466. operate_and_get_next (count, c)
  467.      int count, c;
  468. {
  469.   int where;
  470.   extern int history_stifled, history_length, max_input_history;
  471.  
  472.   /* Accept the current line. */
  473.   rl_newline ();    
  474.  
  475.   /* Find the current line, and find the next line to use. */
  476.   where = where_history ();
  477.  
  478.   if (history_stifled && (history_length >= max_input_history))
  479.     saved_history_line_to_use = where;
  480.   else
  481.     saved_history_line_to_use = where + 1;
  482.  
  483.   rl_startup_hook = (Function *)set_saved_history;
  484. }
  485.  
  486. /* **************************************************************** */
  487. /*                                    */
  488. /*            How To Do Shell Completion            */
  489. /*                                    */
  490. /* **************************************************************** */
  491.  
  492. /* Do some completion on TEXT.  The indices of TEXT in RL_LINE_BUFFER are
  493.    at START and END.  Return an array of matches, or NULL if none. */
  494. static char **
  495. attempt_shell_completion (text, start, end)
  496.      char *text;
  497.      int start, end;
  498. {
  499.   int in_command_position = 0;
  500.   char **matches = (char **)NULL;
  501.   char *command_separator_chars = ";|&{(";
  502.  
  503.   /* Determine if this could be a command word.  It is if it appears at
  504.      the start of the line (ignoring preceding whitespace), or if it
  505.      appears after a character that separates commands.  It cannot be a
  506.      command word if we aren't at the top-level prompt. */
  507.   {
  508.     register int ti = start - 1;
  509.  
  510.     while ((ti > -1) && (whitespace (rl_line_buffer[ti])))
  511.       ti--;
  512.  
  513.     if (ti < 0)
  514.       {
  515.     extern char *current_prompt_string, *ps1_prompt;
  516.  
  517.     /* Only do command completion at the start of a line when we
  518.        are not prompting at top level. */
  519.     if (current_prompt_string == ps1_prompt)
  520.       in_command_position++;
  521.       }
  522.     else
  523.       {
  524.     if (member (rl_line_buffer[ti], command_separator_chars))
  525.       in_command_position++;
  526.       }
  527.   }
  528.  
  529.   /* Variable name? */
  530.   if (*text == '$')
  531.     matches = completion_matches (text, variable_completion_function);
  532.  
  533.   /* If the word starts in `~', and there is no slash in the word, then
  534.      try completing this word as a username. */
  535.   if (!matches && *text == '~' && !index (text, '/'))
  536.     matches = completion_matches (text, username_completion_function);
  537.  
  538.   /* Another one.  Why not?  If the word starts in '@', then look through
  539.      the world of known hostnames for completion first. */
  540.   if (!matches && *text == '@')
  541.     matches = completion_matches (text, hostname_completion_function);
  542.  
  543.   /* And last, (but not least) if this word is in a command position, then
  544.      complete over possible command names, including aliases, functions,
  545.      and command names. */
  546.   if (!matches && in_command_position && *text != '/')
  547.     matches = completion_matches (text, command_word_completion_function);
  548.  
  549.   return (matches);
  550. }
  551.  
  552. /* This is the function to call when the word to complete is at the start
  553.    of a line.  It grovels $PATH, looking for commands that match.  It also
  554.    scans aliases, function names, and the shell_builtin table. */
  555. static char *
  556. command_word_completion_function (hint_text, state)
  557.      char *hint_text;
  558.      int state;
  559. {
  560.   extern SHELL_VAR **all_visible_functions ();
  561.   char *extract_colon_unit ();
  562.   static char *hint = (char *)NULL;
  563.   static char *path = (char *)NULL;
  564.   static char *val = (char *)NULL;
  565.   static char *filename_hint = (char *)NULL;
  566.   static int path_index, hint_len, istate;
  567.   static int mapping_over, local_index;
  568.   static SHELL_VAR **varlist = (SHELL_VAR **)NULL;
  569.  
  570.   /* We have to map over the possibilities for command words.  If we have
  571.      no state, then make one just for that purpose. */
  572.  
  573.   if (!state)
  574.     {
  575.       if (hint)
  576.     free (hint);
  577.  
  578.       path = get_string_value ("PATH");
  579.       path_index = 0;
  580.  
  581.       hint = savestring (hint_text);
  582.       hint_len = strlen (hint);
  583.  
  584.       mapping_over = 0;
  585.       val = (char *)NULL;
  586.  
  587.       /* Initialize the variables for each type of command word. */
  588.       local_index = 0;
  589.  
  590.       if (varlist)
  591.     free (varlist);
  592.  
  593.       varlist = all_visible_functions ();
  594.     }
  595.  
  596.   /* mapping_over says what we are currently hacking.  Note that every case
  597.      in this list must fall through when there are no more possibilities. */
  598.  
  599.   switch (mapping_over)
  600.     {
  601.     case 0:            /* Aliases come first. */
  602. #if defined (ALIAS)
  603.       while (aliases && aliases[local_index])
  604.     {
  605.       register char *alias;
  606.  
  607.       alias = aliases[local_index++]->name;
  608.  
  609.       if (strncmp (alias, hint, hint_len) == 0)
  610.         return (savestring (alias));
  611.     }
  612. #endif /* ALIAS */
  613.       local_index = 0;
  614.       mapping_over++;
  615.  
  616.     case 1:            /* Then shell reserved words. */
  617.       {
  618.     extern STRING_INT_ALIST word_token_alist[];
  619.  
  620.     while (word_token_alist[local_index].word)
  621.       {
  622.         register char *reserved_word;
  623.  
  624.         reserved_word = word_token_alist[local_index++].word;
  625.  
  626.         if (strncmp (reserved_word, hint, hint_len) == 0)
  627.           return (savestring (reserved_word));
  628.       }
  629.     local_index = 0;
  630.     mapping_over++;
  631.       }
  632.  
  633.     case 2:            /* Then function names. */
  634.       while (varlist && varlist[local_index])
  635.     {
  636.       register char *varname;
  637.  
  638.       varname = varlist[local_index++]->name;
  639.  
  640.       if (strncmp (varname, hint, hint_len) == 0)
  641.         return (savestring (varname));
  642.     }
  643.       local_index = 0;
  644.       mapping_over++;
  645.  
  646.     case 3:            /* Then shell builtins. */
  647.       for (; shell_builtins[local_index].name; local_index++)
  648.     {
  649.       if (!shell_builtins[local_index].function ||
  650.           !shell_builtins[local_index].enabled)
  651.         continue;
  652.  
  653.       if (strncmp
  654.           (shell_builtins[local_index++].name, hint, hint_len) == 0)
  655.         return (savestring (shell_builtins[local_index].name));
  656.     }
  657.       local_index = 0;
  658.       mapping_over++;
  659.     }
  660.  
  661.   /* Repeatedly call filename_completion_function while we have
  662.      members of PATH left.  Question:  should we stat each file?
  663.      Answer: we call executable_file () on each file. */
  664.  outer:
  665.  
  666.   istate = (val != (char *)NULL);
  667.  
  668.   if (!istate)
  669.     {
  670.       char *current_path;
  671.  
  672.       /* Get the next directory from the path.  If there is none, then we
  673.      are all done. */
  674.       if (!path ||
  675.       !path[path_index] ||
  676.       !(current_path = extract_colon_unit (path, &path_index)))
  677.     return ((char *)NULL);
  678.  
  679.       if (!*current_path)
  680.     {
  681.       free (current_path);
  682.       current_path = savestring (".");
  683.     }
  684.  
  685.       if (*current_path == '~')
  686.     {
  687.       extern char *tilde_expand ();
  688.       char *t;
  689.  
  690.       t = tilde_expand (current_path);
  691.       free (current_path);
  692.       current_path = t;
  693.     }
  694.  
  695.       if (filename_hint)
  696.     free (filename_hint);
  697.  
  698.       filename_hint =
  699.     (char *)xmalloc (2 + strlen (current_path)
  700.              + strlen (hint));
  701.       sprintf (filename_hint, "%s/%s", current_path, hint);
  702.  
  703.       free (current_path);
  704.     }
  705.  
  706.  inner:
  707.   val = filename_completion_function (filename_hint, istate);
  708.   istate = 1;
  709.  
  710.   if (!val)
  711.     {
  712.       goto outer;
  713.     }
  714.   else
  715.     {
  716.       char *rindex (), *temp = rindex (val, '/');
  717.       temp++;
  718.       if ((strncmp (hint, temp, hint_len) == 0) && executable_file (val))
  719.     {
  720.       temp = (savestring (temp));
  721.       free (val);
  722.       val = "";    /* So it won't be NULL. */
  723.       return (temp);
  724.     }
  725.       else
  726.     {
  727.       free (val);
  728.       goto inner;
  729.     }
  730.     }
  731. }
  732.  
  733. /* Okay, now we write the entry_function for variable completion. */
  734. static char *
  735. variable_completion_function (text, state)
  736.      int state;
  737.      char *text;
  738. {
  739.   register SHELL_VAR *var = (SHELL_VAR *)NULL;
  740.   static SHELL_VAR **varlist = (SHELL_VAR **)NULL;
  741.   static int varlist_index;
  742.   static char *varname = (char *)NULL;
  743.   static int namelen;
  744.   static int first_char, first_char_loc;
  745.  
  746.   extern SHELL_VAR **all_visible_variables ();
  747.  
  748.   if (!state)
  749.     {
  750.       if (varname)
  751.     free (varname);
  752.  
  753.       first_char_loc = 0;
  754.       first_char = text[0];
  755.  
  756.       if (first_char == '$')
  757.     first_char_loc++;
  758.  
  759.       varname = savestring (&text[first_char_loc]);
  760.  
  761.       namelen = strlen (varname);
  762.       if (varlist)
  763.     free (varlist);
  764.       varlist = all_visible_variables ();
  765.       varlist_index = 0;
  766.     }
  767.  
  768.   while (varlist && varlist[varlist_index])
  769.     {
  770.       var = varlist[varlist_index];
  771.  
  772.       /* Compare.  You can't do better than Zayre.  No text is also
  773.      a match.  */
  774.       if (!*varname || (strncmp (varname, var->name, namelen) == 0))
  775.     break;
  776.       varlist_index++;
  777.     }
  778.  
  779.   if (!varlist || !varlist[varlist_index])
  780.     {
  781.       return ((char *)NULL);
  782.     }
  783.   else
  784.     {
  785.       char *value = (char *)xmalloc (2 + strlen (var->name));
  786.  
  787.       if (first_char_loc)
  788.     *value = first_char;
  789.  
  790.       strcpy (&value[first_char_loc], var->name);
  791.  
  792.       varlist_index++;
  793.       return (value);
  794.     }
  795. }
  796.  
  797. /* How about a completion function for hostnames? */
  798. static char *
  799. hostname_completion_function (text, state)
  800.      int state;
  801.      char *text;
  802. {
  803.   static char **list = (char **)NULL;
  804.   static int list_index = 0;
  805.   static int first_char, first_char_loc;
  806.  
  807.   /* If we don't have any state, make some. */
  808.   if (!state)
  809.     {
  810.       char **hostnames_matching ();
  811.  
  812.       if (list)
  813.     free (list);
  814.  
  815.       list = (char **)NULL;
  816.  
  817.       first_char_loc = 0;
  818.       first_char = *text;
  819.  
  820.       if (first_char == '@')
  821.     first_char_loc++;
  822.  
  823.       list = hostnames_matching (&text[first_char_loc]);
  824.       list_index = 0;
  825.     }
  826.  
  827.   if (list && list[list_index])
  828.     {
  829.       char *t = (char *)xmalloc (2 + strlen (list[list_index]));
  830.  
  831.       *t = first_char;
  832.       strcpy (t + first_char_loc, list[list_index]);
  833.       list_index++;
  834.       return (t);
  835.     }
  836.   else
  837.     return ((char *)NULL);
  838. }
  839.  
  840. /* History and alias expand the line.  But maybe do more?  This
  841.    is a test to see what users like.  Do expand_string on the string. */
  842. static void
  843. shell_expand_line (ignore)
  844.      int ignore;
  845. {
  846.   char *pre_process_line (), *new_line;
  847.  
  848.   new_line = pre_process_line (rl_line_buffer, 0, 0);
  849.  
  850. #if defined (ALIAS)
  851.   if (new_line)
  852.     {
  853.       char *alias_expand (), *alias_line;
  854.  
  855.       alias_line = alias_expand (new_line);
  856.       free (new_line);
  857.       new_line = alias_line;
  858.     }
  859. #endif /* ALIAS */
  860.  
  861.   if (new_line)
  862.     {
  863.       int old_point = rl_point;
  864.       int at_end = rl_point == rl_end;
  865.  
  866.       /* If the line was history and alias expanded, then make that
  867.      be one thing to undo. */
  868.  
  869.       if (strcmp (new_line, rl_line_buffer) != 0)
  870.     {
  871.       rl_point = rl_end;
  872.  
  873.       rl_add_undo (UNDO_BEGIN, 0, 0, 0);
  874.       rl_kill_text (0, rl_point);
  875.       rl_point = rl_end = 0;
  876.       rl_insert_text (new_line);
  877.       rl_add_undo (UNDO_END, 0, 0, 0);
  878.     }
  879.  
  880.       free (new_line);
  881.  
  882.       /* If there is variable expansion to perform, do that as a separate
  883.      operation to be undone. */
  884.       {
  885.     extern char *string_list ();
  886.     extern WORD_LIST *expand_string ();
  887.     WORD_LIST *expanded_string;
  888.     char *string_list ();
  889.  
  890.     expanded_string = expand_string (rl_line_buffer, 0);
  891.     if (!expanded_string)
  892.       new_line = savestring ("");
  893.     else
  894.       {
  895.         new_line = string_list (expanded_string);
  896.         dispose_words (expanded_string);
  897.       }
  898.  
  899.       if (strcmp (new_line, rl_line_buffer) != 0)
  900.     {
  901.       rl_add_undo (UNDO_BEGIN, 0, 0 ,0);
  902.       rl_kill_text (0, rl_end);
  903.       rl_point = rl_end = 0;
  904.       rl_insert_text (new_line);
  905.       rl_add_undo (UNDO_END, 0, 0, 0);
  906.     }
  907.  
  908.       free (new_line);
  909.  
  910.       /* Place rl_point where we think it should go. */
  911.       if (at_end)
  912.     rl_point = rl_end;
  913.       else if (old_point < rl_end)
  914.     {
  915.       rl_point = old_point;
  916.       if (!whitespace (rl_line_buffer[rl_point]))
  917.         rl_forward_word (1);
  918.     }
  919.       }
  920.     }
  921.   else
  922.     {
  923.       /* There was an error in expansion.  Let the preprocessor print
  924.      the error here.  Note that we know that pre_process_line ()
  925.      will return NULL, since it just did. */
  926.       fprintf (rl_outstream, "\n\r");
  927.       pre_process_line (rl_line_buffer, 1, 0);
  928.       rl_forced_update_display ();
  929.     }
  930. }
  931.  
  932. /* Filename completion ignore.  Emulates the "fignore" facility of
  933.    tcsh.  If FIGNORE is set, then don't match files with the
  934.    given suffixes.  If only one of the possibilities has an acceptable
  935.    suffix, delete the others, else just return and let the completer
  936.    signal an error.  It is called by the completer when real
  937.    completions are done on filenames by the completer's internal
  938.    function, not for completion lists (M-?) and not on "other"
  939.    completion types, such as hostnames or commands.
  940.  
  941.    It is passed a NULL-terminated array of (char *)'s that must be
  942.    free()'d if they are deleted.  The first element (names[0]) is the
  943.    least-common-denominator string of the matching patterns (i.e.
  944.    u<TAB> produces names[0] = "und", names[1] = "under.c", names[2] =
  945.    "undun.c", name[3] = NULL).  */
  946.  
  947. struct ign {
  948.   char *val;
  949.   int len;
  950. };
  951.  
  952. static struct ign *ignores;    /* Store the ignore strings here */
  953. static int num_ignores;        /* How many are there? */
  954. static char *last_fignore;    /* Last value of fignore - cached for speed */
  955. extern char *extract_colon_unit (), *get_string_value ();
  956.  
  957. static void
  958. setup_ignore_patterns ()
  959. {
  960.   int numitems, maxitems, ptr;
  961.   char *colon_bit;
  962.   struct ign *p;
  963.   
  964.   char *this_fignore = get_string_value ("FIGNORE");
  965.  
  966.   /* If nothing has changed then just exit now. */
  967.   if (this_fignore &&
  968.       last_fignore && 
  969.       strcmp (this_fignore, last_fignore) == 0 ||
  970.       (!this_fignore && !last_fignore))
  971.     {
  972.       return;
  973.     }
  974.  
  975.   /* Oops.  FIGNORE has changed.  Re-parse it. */
  976.   num_ignores = 0;
  977.  
  978.   if (ignores)
  979.     {
  980.       for (p = ignores; p->val; p++) free(p->val);
  981.       free (ignores);
  982.       ignores = (struct ign*)NULL;
  983.     }
  984.  
  985.   if (last_fignore)
  986.     free (last_fignore);
  987.  
  988.   last_fignore = savestring (this_fignore);
  989.   
  990.   if (!this_fignore)
  991.     return;
  992.  
  993.   numitems = maxitems = ptr = 0;
  994.  
  995.   while (colon_bit = extract_colon_unit (this_fignore, &ptr))
  996.     {
  997.       if (numitems + 1 > maxitems)
  998.     ignores = (struct ign *)
  999.       xrealloc (ignores, (maxitems += 10) * sizeof (struct ign));
  1000.  
  1001.       ignores[numitems].val = colon_bit;
  1002.       ignores[numitems].len = strlen (colon_bit);
  1003.       numitems++;
  1004.     }
  1005.   ignores[numitems].val = NULL;
  1006.   num_ignores = numitems;
  1007. }
  1008.  
  1009. static int
  1010. name_is_acceptable (name)
  1011.      char *name;
  1012. {
  1013.   struct ign *p;
  1014.   int nlen = strlen (name);
  1015.  
  1016.   for (p = ignores; p->val; p++) 
  1017.     {
  1018.       if (nlen > p->len && p->len > 0 && 
  1019.       strcmp (p->val, &name[nlen - p->len]) == 0)
  1020.     return (0);
  1021.     }
  1022.  
  1023.   return (1);
  1024. }
  1025.  
  1026. static void
  1027. filename_completion_ignore (names)
  1028.      char **names;
  1029. {
  1030.   char **p;
  1031.   int idx;
  1032.  
  1033.   setup_ignore_patterns ();
  1034.  
  1035.   if (!num_ignores)
  1036.     return;
  1037.   
  1038.   for (p = names + 1, idx = -1; *p; p++)
  1039.     {
  1040.       if (name_is_acceptable (*p))
  1041.     {
  1042.       if (idx == -1)    /* First match found. */
  1043.         idx = p - names;
  1044.       else
  1045.         return;        /* Too many matches. */
  1046.     }
  1047.     }
  1048.   
  1049.   /* If none are acceptable then let the completer handle it. */
  1050.   if (idx == -1)
  1051.     return;
  1052.  
  1053.   /* Delete all non-matching elements. */
  1054.   free (names[0]); 
  1055.   for (p = names + 1; *p; p++)
  1056.     {
  1057.       if (idx == (p - names))
  1058.     names[0] = *p;
  1059.       else 
  1060.     free (*p);
  1061.  
  1062.       *p = NULL;
  1063.     }
  1064. }
  1065.  
  1066. /* Handle symbolic link references while hacking completion. */
  1067. static void
  1068. bash_symbolic_link_hook (dirname)
  1069.      char **dirname;
  1070. {
  1071.   extern int follow_symbolic_links;
  1072.   char *make_absolute (), *temp_dirname;
  1073.  
  1074.   if (follow_symbolic_links && (strcmp (*dirname, ".") != 0))
  1075.     {
  1076.       temp_dirname = make_absolute (*dirname, get_working_directory (""));
  1077.  
  1078.       if (temp_dirname)
  1079.     {
  1080.       free (*dirname);
  1081.       *dirname = temp_dirname;
  1082.     }
  1083.     }
  1084. }
  1085.  
  1086. #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
  1087. static void
  1088. bash_complete_username (ignore, ignore2)
  1089.      int ignore, ignore2;
  1090. {
  1091.   bash_complete_username_internal (TAB);
  1092. }
  1093.  
  1094. static void
  1095. bash_possible_username_completions (ignore, ignore2)
  1096.      int ignore, ignore2;
  1097. {
  1098.   bash_complete_username_internal ('?');
  1099. }
  1100.  
  1101. static void
  1102. bash_complete_username_internal (what_to_do)
  1103.      int what_to_do;
  1104. {
  1105.   bash_specific_completion
  1106.     (what_to_do, (Function *)username_completion_function);
  1107. }
  1108.  
  1109. static void
  1110. bash_complete_filename (ignore, ignore2)
  1111.      int ignore, ignore2;
  1112. {
  1113.   bash_complete_filename_internal (TAB);
  1114. }
  1115.  
  1116. static void
  1117. bash_possible_filename_completions (ignore, ignore2)
  1118.      int ignore, ignore2;
  1119. {
  1120.   bash_complete_filename_internal ('?');
  1121. }
  1122.  
  1123. static void
  1124. bash_complete_filename_internal (what_to_do)
  1125.      int what_to_do;
  1126. {
  1127.   Function  *orig_func;
  1128.   Function *orig_attempt_func;
  1129.   char *orig_rl_completer_word_break_characters;
  1130.   extern char *rl_completer_word_break_characters;
  1131.  
  1132.   orig_func = rl_completion_entry_function;
  1133.   orig_attempt_func = rl_attempted_completion_function;
  1134.   orig_rl_completer_word_break_characters = rl_completer_word_break_characters;
  1135.   rl_completion_entry_function = (Function *)filename_completion_function;
  1136.   rl_attempted_completion_function = (Function *)0x0;
  1137.   rl_completer_word_break_characters = " \t\n\"\'";
  1138.  
  1139.   rl_complete_internal (what_to_do);
  1140.  
  1141.   rl_completion_entry_function = orig_func;
  1142.   rl_attempted_completion_function = orig_attempt_func;
  1143.   rl_completer_word_break_characters = orig_rl_completer_word_break_characters;
  1144. }
  1145.  
  1146. static void
  1147. bash_complete_hostname (ignore, ignore2)
  1148.      int ignore, ignore2;
  1149. {
  1150.   bash_complete_hostname_internal (TAB);
  1151. }
  1152.  
  1153. static void
  1154. bash_possible_hostname_completions (ignore, ignore2)
  1155.      int ignore, ignore2;
  1156. {
  1157.   bash_complete_hostname_internal ('?');
  1158. }
  1159.  
  1160. static void
  1161. bash_complete_variable (ignore, ignore2)
  1162.      int ignore, ignore2;
  1163. {
  1164.   bash_complete_variable_internal (TAB);
  1165. }
  1166.  
  1167. static void
  1168. bash_possible_variable_completions (ignore, ignore2)
  1169.      int ignore, ignore2;
  1170. {
  1171.   bash_complete_variable_internal ('?');
  1172. }
  1173.  
  1174.  
  1175. static void
  1176. bash_complete_command (ignore, ignore2)
  1177.      int ignore, ignore2;
  1178. {
  1179.   bash_complete_command_internal (TAB);
  1180. }
  1181.  
  1182. static void
  1183. bash_possible_command_completions (ignore, ignore2)
  1184.      int ignore, ignore2;
  1185. {
  1186.   bash_complete_command_internal ('?');
  1187. }
  1188.  
  1189. static void
  1190. bash_complete_hostname_internal (what_to_do)
  1191.      int what_to_do;
  1192. {
  1193.   bash_specific_completion
  1194.     (what_to_do, (Function *)hostname_completion_function);
  1195. }
  1196.  
  1197. static void
  1198. bash_complete_variable_internal (what_to_do)
  1199.      int what_to_do;
  1200. {
  1201.   bash_specific_completion
  1202.     (what_to_do, (Function *)variable_completion_function);
  1203. }
  1204.  
  1205. static void
  1206. bash_complete_command_internal (what_to_do)
  1207.      int what_to_do;
  1208. {
  1209.   bash_specific_completion
  1210.     (what_to_do, (Function *)command_word_completion_function);
  1211. }
  1212.  
  1213. static void
  1214. bash_specific_completion (what_to_do, generator)
  1215.      int what_to_do;
  1216.      Function *generator;
  1217. {
  1218.   Function *orig_func;
  1219.   Function *orig_attempt_func;
  1220.  
  1221.   orig_func = rl_completion_entry_function;
  1222.   orig_attempt_func = rl_attempted_completion_function;
  1223.   rl_completion_entry_function = generator;
  1224.   rl_attempted_completion_function = (Function *)0x0;
  1225.  
  1226.   rl_complete_internal (what_to_do);
  1227.  
  1228.   rl_completion_entry_function = orig_func;
  1229.   rl_attempted_completion_function = orig_attempt_func;
  1230. }
  1231.  
  1232. #endif    /* SPECIFIC_COMPLETION_FUNCTIONS */
  1233.  
  1234.